﻿/*
 * @author Lee 
 * Time: 20180403 
 * Abstract: SharePoint API的二次封装
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace SharePointHelper.DLL.SSOM
{
	public class ContentType
	{
        #region 判断content type是否存在
        /// <summary>
        /// 判断content type是否存在
        /// http://msdn.microsoft.com/en-us/library/aa543576.aspx
        /// </summary>
        /// <param name="web"></param>
        /// <param name="contentTypeName"></param>
        /// <returns></returns>
        public static bool IsExist(SPWeb web, string contentTypeName)
		{
			bool isExist = false;
			try
			{
				if (web == null) { throw new Exception("SPWeb passed is null"); }

				SPContentType ct = web.AvailableContentTypes["Financial Document"];
				if (ct != null) { isExist = true; }
			}
			catch (Exception ex) { Log.WriteError("ersdr6ujrdtr", ex); }
			return isExist;
		}
        #endregion

        #region 创建一个新的 content type
        /// <summary>
        /// 创建一个新的 content type
        /// </summary>
        /// <param name="web"></param>
        /// <param name="name">Content Type Name</param>
        /// <param name="description"></param>
        /// <param name="group">Group name the content type will be saved under</param>
        /// <param name="parentContentType"></param>
        /// <param name="newTitleDisplayNameToChangeTo">Use this if want to change the default column "Title" to something else, such as "Fullname"</param>
        /// <param name="siteColumnsByInternalNames">Array of existing columns to add to this content type</param>
        /// <returns></returns>
        public static SPContentType CreateContentType(SPWeb web, string name, string description, string group, SPContentType parentContentType,
			string newTitleDisplayNameToChangeTo, string[] siteColumnsByInternalNames)
		{
			SPContentType contentType = null;
			try
			{
				// init
				if (web == null) { throw new Exception("SPWeb passed is null"); }
				if (parentContentType == null) { parentContentType = web.ContentTypes["Item"]; }
				if (string.IsNullOrEmpty(group)) { throw new Exception("Site Content Type group name not defined"); }
				if (IsExist(web, name)) { throw new Exception("Content Type with the same name already exists: " + name); }


				// create content type
				contentType = new SPContentType(parentContentType, web.ContentTypes, name);
				contentType.Description = description;
				contentType.Group = group;
				web.ContentTypes.Add(contentType);

				// change display name of Title field
				if (!string.IsNullOrEmpty(newTitleDisplayNameToChangeTo))
				{
					SPFieldLink fieldTitle = contentType.FieldLinks["Title"];
					fieldTitle.DisplayName = newTitleDisplayNameToChangeTo;
				}

				// add fields
				if (siteColumnsByInternalNames != null)
				{
					foreach (string internalName in siteColumnsByInternalNames)
					{
						SPField field = web.Fields.GetFieldByInternalName(internalName);
						if (field == null) { throw new Exception("Failed to get site column with display name: " + internalName); }

						SPFieldLink fieldLink = new SPFieldLink(field);
						// set properties, such as required, etc.
						contentType.FieldLinks.Add(fieldLink);
					}
				}
				contentType.Update();
			}
			catch (Exception ex)
			{
				Log.WriteError("w6555r4hthsfzdv35y", ex);
				contentType = null;
			}
			return contentType;
		}
        #endregion

        #region 获取一个item的content type的名字
        /// <summary>
        /// 获取一个item的content type的名字
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static string GetContentTypeName(SPListItem item)
		{
			string o = null;
			if (item != null)
			{
				if (!Field.IsExistAndFetched(item, "ContentType")) { Log.WriteError("345546r58reetssw", "item: '" + item.Title + "', does not have ContentType fetched, therefore cannot determine type"); }
				try
				{
					o = (string)item["ContentType"];

					if (string.IsNullOrEmpty(o) && item.ContentType != null && !string.IsNullOrEmpty(item.ContentType.Name))
					{
						o = item.ContentType.Name;
					}
				}
				catch (Exception ex)
				{
					Log.WriteError("rtyryityrssreseresrertdf", ex);
				}
			}

			return o;
		}
        #endregion
    }		
}
